package com.llg.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.llg.common.BaseContext;
import com.llg.common.R;
import com.llg.dto.OrdersDto;
import com.llg.entity.Orders;
import com.llg.entity.User;
import com.llg.service.OrderService;
import com.llg.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/order")
@Slf4j
public class OrderController {

    @Autowired
    private OrderService orderService;

    @Autowired
    private UserService userService;

    @PostMapping("/submit")
    public R<String> submit(@RequestBody Orders orders){
        orderService.submit(orders);

        return R.success("提交订单成功");
    }

    /**
     * 分页查询订单
     * @param page
     * @param pageSize
     * @param number
     * @param beginTime
     * @param endTime
     * @return
     */
    @GetMapping("/page")
    public R<Page<OrdersDto>> page(int page, int pageSize, Long number, String beginTime, String endTime, HttpServletRequest request){
        //构造分页器对象
        Page<Orders> pageInfo = new Page<>(page, pageSize);
        Page<OrdersDto> resPage = new Page<>();

        //将String类型的开始时间转localdatetime类型
        LocalDateTime sTime = null;
        LocalDateTime eTime = null;
        if(beginTime != null) {
            sTime = LocalDateTime.parse(beginTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            log.info("stime=",sTime);
        }
        if(endTime != null) {
            eTime = LocalDateTime.parse(endTime, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            log.info("etime=", eTime);
        }
        log.info("sTime=>{}",sTime);
        log.info("eTime=>{}",eTime);
        //构造条件查询
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(number != null,Orders::getNumber,number);
        queryWrapper.gt(beginTime != null,Orders::getOrderTime,sTime);
        queryWrapper.lt(endTime != null,Orders::getOrderTime,eTime);
        orderService.page(pageInfo,queryWrapper);

        BeanUtils.copyProperties(pageInfo,resPage,"records");
        List<OrdersDto> list = new ArrayList<>();
        List<Orders> records = pageInfo.getRecords();
        for (Orders record : records) {
            OrdersDto ordersDto = new OrdersDto();
            BeanUtils.copyProperties(record,ordersDto);
            Long userId = record.getUserId();
            User user = userService.getById(userId);
            if(user.getName() != null)ordersDto.setUserName(user.getName());
            else ordersDto.setUserName(user.getPhone());
            list.add(ordersDto);

        }

        resPage.setRecords(list);

        return R.success(resPage);
    }

    /**
     * 更改订单状态
     * @param orders
     * @return
     */
    @PutMapping
    public R<String> update(@RequestBody Orders orders){
        orderService.updateById(orders);
        return R.success("派送成功");
    }

    @GetMapping("/userPage")
    public R<Page<Orders>> userPage(int page,int pageSize){
        Page<Orders> pageInfo = new Page<>();
        LambdaQueryWrapper<Orders> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Orders::getUserId,BaseContext.getCurrentId());
        orderService.page(pageInfo,queryWrapper);
        return R.success(pageInfo);
    }

    @PostMapping("/again")
    public R<Orders> again(@RequestBody Orders orders){
        Orders res = orderService.getById(orders);
        return R.success(res);
    }
}
